package com.chakid.mobileplay2.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.chakid.mobileplay2.R;
import com.chakid.mobileplay2.base.BasePager;
import com.chakid.mobileplay2.common.RepalceFragment;
import com.chakid.mobileplay2.pager.AudioPager;
import com.chakid.mobileplay2.pager.NetAudioPager;
import com.chakid.mobileplay2.pager.NetVideoPager;
import com.chakid.mobileplay2.pager.VideoPager;

import java.util.ArrayList;


/**
 * Created by Yuan on 2018/10/12.
 * 主页面
 */

public class MainActivity extends FragmentActivity{


    private RadioGroup rg_bottom_tag;

    //添加页面集合

    private ArrayList<BasePager> basePagers;

    //底部导航栏选中的位置
    private int position;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        rg_bottom_tag= findViewById(R.id.rg_bottom_tag);


        basePagers = new ArrayList<>();
        basePagers.add(new VideoPager(this)); //添加本地视频页面
        basePagers.add(new AudioPager(this)); //添加音乐视频页面
        basePagers.add(new NetVideoPager(this)); //添加网络视频页面
        basePagers.add(new NetAudioPager(this)); //添加网络音乐页面

        //设置RadioGroup的监听
        rg_bottom_tag.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        rg_bottom_tag.check(R.id.rb_video);//默认选中首页

    }



    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

            switch (checkedId){
                default:
                    position = 0;
                    break;
                case R.id.rb_audio:
                    position = 1;
                    break;
                case R.id.rb_net_video:
                    position = 2;
                    break;
                case R.id.rb_net_audio:
                    position = 3;
                    break;
            }
            setFragment();
        }
    }



    /**
     * 将页面添加到Fragment中
     */

    private void setFragment(){

        //1、得到FragmentManger
        FragmentManager manager= getSupportFragmentManager();

        //2、开启事务
        FragmentTransaction ft= manager.beginTransaction();

        //3、替换
        ft.replace(R.id.fl_main_content,new RepalceFragment(getBasePager()));
        //4、提交事务
        ft.commit();

    }

    public BasePager getBasePager() {

        BasePager basePager = basePagers.get(position);
        if(basePager != null&&!basePager.isInitData){
            basePager.initData();//进行联网请求或者数据绑定
            basePager.isInitData =true;
        }
        return basePager;
    }

}
